// CToggleButton.cpp : Implementierungsdatei
//

#include "stdafx.h"
#include "Charplan.h"
#include "CToggleButton.h"
#include ".\ctogglebutton.h"


// CToggleButton

IMPLEMENT_DYNAMIC(CToggleButton, CButton)
CToggleButton::CToggleButton()
{
    mb_pushed = false;
    mb_highlit = false;
    mw_addition_message_target = NULL;
}

CToggleButton::~CToggleButton()
{
}


BEGIN_MESSAGE_MAP(CToggleButton, CButton)
    ON_CONTROL_REFLECT(BN_CLICKED, OnBnClicked)
    ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()



// CToggleButton-Meldungshandler


void CToggleButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    CDC  *dc;
    CRect button_rect, text_rect, offset_text_rect;
    UINT state;

    dc    = CDC::FromHandle(lpDrawItemStruct->hDC);
    state = lpDrawItemStruct->itemState;

    button_rect.CopyRect(&lpDrawItemStruct->rcItem);

    text_rect = button_rect;
    offset_text_rect = text_rect;
    offset_text_rect.OffsetRect(1, 1);


    // Retrieve the button's caption
    const int bufSize = 512;
    TCHAR buffer[bufSize];
    GetWindowText(buffer, bufSize);


    //Background
    dc->FillSolidRect(button_rect,GetSysColor(COLOR_BTNFACE));

    if (state & ODS_SELECTED ^ mb_pushed)
    {
        // pushed
        //dc->DrawFrameControl(button_rect, DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_PUSHED);
        dc->FillSolidRect(text_rect,RGB(192,192,255));
        DrawButtonText(dc, offset_text_rect, buffer, GetSysColor(COLOR_BTNTEXT));
    }
    else
    {
        dc->DrawFrameControl(button_rect, DFC_BUTTON, DFCS_BUTTONPUSH);
        if (mb_highlit)
        {
            DrawButtonText(dc, text_rect, buffer, RGB(32,32,255));
        }
        else
        {
            DrawButtonText(dc, text_rect, buffer, GetSysColor(COLOR_BTNTEXT));
        }
    }
}


void CToggleButton::DrawButtonText(CDC *dc, CRect r, const char *Buf, COLORREF text_colour)
{
    COLORREF previous_colour;

    previous_colour = dc->SetTextColor(text_colour);
    dc->SetBkMode(TRANSPARENT);
    dc->DrawText(Buf, (int)strlen(Buf), r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
    dc->SetTextColor(previous_colour);
}

BOOL CToggleButton::PreCreateWindow(CREATESTRUCT& cs)
{
    cs.style |= BS_OWNERDRAW;

    return CButton::PreCreateWindow(cs);
}

void CToggleButton::SetPushed(bool val)
{
    if (mb_pushed == val) return;
    mb_pushed = val;
    Invalidate();
}


void CToggleButton::SetHighlit(bool val)
{
    if (mb_highlit == val) return;
    mb_highlit = val;
    Invalidate();
}


void CToggleButton::OnBnClicked()
{
    mb_pushed = !mb_pushed;
    Invalidate();

    if (mw_addition_message_target)
    {
        mw_addition_message_target->PostMessage(WM_COMMAND,
                                                MAKELONG(GetDlgCtrlID(), BN_CLICKED),
                                                (LPARAM)m_hWnd);
    }
}

void CToggleButton::OnMouseMove(UINT nFlags, CPoint point)
{
    CRect rect;
    GetWindowRect(rect);
    GetParent()->ScreenToClient(rect);
    GetParent()->SendMessage(WM_MOUSEMOVE,
                             nFlags,
                             (LPARAM) MAKELONG(rect.left + point.x, rect.top + point.y));
    CButton::OnMouseMove(nFlags, point);
}
